home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / code / contr / setup.exe / Disk1 / data1.cab / Configuration_En / Commands / InsertFlashPaper.js < prev    next >
Encoding:
JavaScript  |  2003-07-18  |  11.6 KB  |  434 lines

  1. // ----------------------------------------------------
  2. //
  3. // Copyright 2003 Macromedia, Inc. All rights reserved.
  4. // ----------------------------------------------------
  5. //
  6. // InsertFlashPaper.js
  7. //
  8. // ----------------------------------------------------
  9.  
  10. // ----------------------------------------------------
  11. // private constants
  12. // ----------------------------------------------------
  13.  
  14. /*const*/ var CONVERSION_CHECK_RATE = 200;    // in milliseconds
  15.  
  16. /*const*/ var FPC_SUCCESS = 0;
  17. /*const*/ var FPC_FAILURE = -1;
  18. /*const*/ var FPC_NO_DOCUMENT_HANDLER = -2;
  19. /*const*/ var FPC_DISK_FULL = -3;
  20. /*const*/ var FPC_DISK_WRITE_ERROR = -4;
  21. /*const*/ var FPC_CANCELED = -5;
  22. /*const*/ var FPC_BAD_ARGS = -6;
  23. /*const*/ var FPC_NO_DRIVER = -7;
  24. /*const*/ var FPC_NO_CONVERSION_BEGUN = -8;
  25. /*const*/ var FPC_CONVERSION_IN_PROGRESS = -9;
  26. /*const*/ var FPC_WAITING_FOR_CONNECTION = -10;
  27.  
  28. function isFpcError(result)
  29. {
  30.     return (result < 0);
  31. }
  32.  
  33. // ----------------------------------------------------
  34. // globals
  35. // ----------------------------------------------------
  36.  
  37. var g_file = null;
  38. var g_retVal = null;
  39. var g_paperSizeInfo = null;
  40.  
  41. // ----------------------------------------------------
  42. // public functions
  43. // ----------------------------------------------------
  44.  
  45. function receiveArguments()
  46. {
  47.     g_file = arguments[0];
  48.     g_retVal = arguments[1];
  49.     g_paperSizeInfo = arguments[2];
  50. }
  51.  
  52. function run()
  53. {
  54.     var result = insertFlashPaper( g_file, g_paperSizeInfo );
  55.     if (g_retVal != null)
  56.         g_retVal[0] = result;
  57. }
  58.  
  59. function isFlashPaperAvailable(warnUserIfNotAvailable)
  60. {
  61.     var status = FPC_NO_DRIVER;
  62.     if (typeof(FlashPaperConnect) != "undefined")
  63.     {
  64.         status = FlashPaperConnect.isAvailable();
  65.     }
  66.     
  67.     if (status == FPC_SUCCESS)
  68.     {
  69.         return true;
  70.     }
  71.     else
  72.     {
  73.         if (warnUserIfNotAvailable)
  74.         {
  75.             if (status == FPC_NO_DRIVER)
  76.             {
  77.                 alert(dw.loadString("flashpaper/flash paper install error"));
  78.             }
  79.             else
  80.             {
  81.                 alert(dw.loadString("flashpaper/flash paper wrong os error"));
  82.             }
  83.         }
  84.         return false;
  85.     }
  86. }
  87.  
  88. function insertFlashPaper(sourceURL, paperSizeInfo)
  89. {
  90.     /*const*/ var warnUserIfNotAvailable = true;
  91.     if (!isFlashPaperAvailable(warnUserIfNotAvailable))
  92.     {
  93.         return false;
  94.     }
  95.  
  96.     if (sourceURL == null || sourceURL == "") 
  97.     {
  98.         return false;
  99.     }
  100.  
  101.     var prefsAccessibilityOption;
  102.     if (dw.appName == "Contribute")
  103.     {
  104.         prefsAccessibilityOption = dw.getAdminEnforceAccessibilityPref();
  105.     }
  106.     else
  107.     {
  108.         prefsAccessibilityOption = dw.getPreferenceString("Accessibility", "Accessibility Image Options", "");
  109.     }
  110.     if (prefsAccessibilityOption)
  111.     {
  112.         var ok = dw.displayOptionalDialog("Flash Accessibility Warning Dialog", 
  113.                                         dw.loadString("insert doc dialog/flash accessibility warning"), 
  114.                                         true);
  115.         if (!ok)
  116.         {
  117.             return false;
  118.         }
  119.     }
  120.  
  121.     // FlashPaperConnect expects a "real" pathname, not a fileURL
  122.     var sourceFile = MMNotes.localURLToFilePath(dw.doURLDecoding(sourceURL));
  123.     if (isFpcError(FlashPaperConnect.canDoConversion(sourceFile)))
  124.     {
  125.         alert(dw.loadString("flashpaper/flash paper conversion error"));
  126.         return false;
  127.     }
  128.  
  129.     var destURL = dw.doURLEncoding(calcUniqueDestURL(sourceURL));
  130.     if (destURL == null || destURL == "")
  131.     {
  132.         alert(dw.loadString("flashpaper/flash paper conversion error"));
  133.         return false;
  134.     }
  135.  
  136.     // package up the info we need to save in the progress callback.
  137.     var destFile = MMNotes.localURLToFilePath(dw.doURLDecoding(destURL));
  138.     var conversionInfo = 
  139.     {
  140.         sourceFile: sourceFile,
  141.         destFile: destFile,
  142.         paperSize: paperSizeInfo,
  143.         sessionID: 0
  144.     };
  145.     var titleString = dw.loadString("flashpaper/flash paper waiting title");
  146.     var waitString = dw.loadString("flashpaper/flash paper waiting message");
  147.     var result = callViaProgressDialog(titleString, waitString, CONVERSION_CHECK_RATE, waitForFlashPrinter, conversionInfo);
  148.  
  149.     // we're done. Tell FPC we're no longer interested in this session,
  150.     // regardless of success, failure, or cancellation. (Ignore any error.)
  151.     // pass 1 for the second argument to force us back into the foreground.
  152.     FlashPaperConnect.endConversion(conversionInfo.sessionID, 1);
  153.  
  154.     if (result == null || result == FPC_CANCELED)
  155.     {
  156.         // user canceled.
  157.         return false;
  158.     }
  159.  
  160.     if (result != FPC_SUCCESS)
  161.     {
  162.         alert(dw.loadString("flashpaper/flash paper conversion error"));
  163.         return false;
  164.     }
  165.  
  166.     var widthAndHeight = dw.extractWidthAndHeightFromSWF(destFile);
  167.     var width = (widthAndHeight != null) ? widthAndHeight[0] : 32;
  168.     var height = (widthAndHeight != null) ? widthAndHeight[1] : 32;
  169.  
  170.     var winWidth = dw.getDocumentDOM().parentWindow.innerWidth;
  171.     if (width > 0 && height > 0 && winWidth > 0)
  172.     {
  173.         /*const*/ var PERCENT_OF_WIDTH = 0.90;
  174.         var aspectRatio = height / width;
  175.         width = Math.round(winWidth * PERCENT_OF_WIDTH);
  176.         height = Math.round(width * aspectRatio);
  177.     }
  178.  
  179.     var rtnStr = '<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + 
  180.                 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ' +
  181.                 'WIDTH="' + width.toString() + '" HEIGHT="' + height.toString() + '" BORDER="1">\n' + 
  182.                 '<PARAM NAME="movie" VALUE="' + destURL + '?POPUP_ENABLED=true"> ' +
  183.                 '<PARAM NAME="menu" VALUE="false"> ' +
  184.                 '<PARAM NAME="quality" VALUE="high">\n' +
  185.                 '<EMBED SRC="' + destURL + '?POPUP_ENABLED=true" ' +
  186.                 'quality="high" menu="false" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" ' +
  187.                 'TYPE="application/x-shockwave-flash" WIDTH="' + width.toString() + '" HEIGHT="' + height.toString() + '">'+
  188.                 '</EMBED></OBJECT>';
  189.  
  190.     prefsAccessibilityOption = dw.getPreferenceString("Accessibility", "Accessibility Media Options", "");
  191.  
  192.     if (prefsAccessibilityOption == 'TRUE')
  193.     {
  194.         rtnStr = addAccessibility(rtnStr);
  195.     }
  196.  
  197.     /*const*/ var bReplaceCurrentSelection = true;
  198.     /*const*/ var bAutoSetEmbeddedItemsWidthAndHeight = false;
  199.     dw.getDocumentDOM().insertHTML(rtnStr, bReplaceCurrentSelection, bAutoSetEmbeddedItemsWidthAndHeight);
  200.     return true;
  201. }
  202.  
  203. // ----------------------------------------------------
  204. // ProgressDialog wrapper functions
  205. // ----------------------------------------------------
  206.  
  207. var g_percentage = -1;
  208. var g_msgString = null;
  209.  
  210. function callViaProgressDialog(title, message, howFrequentlyToCall, callbackFunction, callbackFunctionArg)
  211. {
  212.     var progress = null;
  213.     var response = null;
  214.     try
  215.     {
  216.         progress = dw.openProgressDialog(title, message, 0);
  217.         if (progress != null)
  218.         {
  219.             var lastCall = 0;
  220.             while (true)
  221.             {
  222.                 var now = MM.getTickCount();
  223.                 if (now < lastCall + howFrequentlyToCall)
  224.                     continue;
  225.  
  226.                 lastCall = now;
  227.  
  228.                 if (callbackFunction != null)
  229.                     response = callbackFunction(updatePercentageFunc, callbackFunctionArg);
  230.                 
  231.                 if (g_msgString != null)
  232.                 {
  233.                     progress.setMessage(g_msgString.toString());
  234.                     g_msgString = null;
  235.                 }
  236.  
  237.                 var canceled = progress.update(g_percentage, 100);
  238.                 if (canceled)
  239.                 {
  240.                     response = null;
  241.                     break;
  242.                 }
  243.  
  244.                  if (response != null) 
  245.                 {
  246.                     break;
  247.                  }
  248.             }        
  249.             
  250.             progress.close();
  251.             progress = null;
  252.         }
  253.     } 
  254.     catch( e ) 
  255.     {
  256.         // just in case.
  257.         if (progress != null)
  258.         {
  259.             progress.close();
  260.             progress = null;
  261.         }
  262.     }
  263.     return response;
  264. }
  265.  
  266.  
  267. function updatePercentageFunc(percentage, msgString)
  268. {
  269.     g_percentage = percentage;
  270.     g_msgString = msgString;
  271. }
  272.  
  273. // ----------------------------------------------------
  274. // private functions
  275. // ----------------------------------------------------
  276.  
  277. function getLeafName(url)
  278. {
  279.     if (url.length > 0 && url[url.length-1] == '/')
  280.         url = url.substr(0, url.length-1);
  281.  
  282.     var lastSlash = url.lastIndexOf("/");
  283.     if (lastSlash != -1)
  284.         return url.substr(lastSlash+1);
  285.     else
  286.         return url;
  287. }
  288.  
  289. function stripExtension(url)
  290. {
  291.     var lastDot = url.lastIndexOf(".");
  292.     if (lastDot >= 1)
  293.         return url.slice(0, lastDot);
  294.     else
  295.         return url;
  296. }
  297.  
  298. function calcUniqueDestURL(sourceURL)
  299. {    
  300.     if (typeof(FileStateManager.getLocalAssetsDirURLForSite) == "undefined")
  301.     {
  302.         return "";
  303.     }
  304.  
  305.     var destDir = FileStateManager.getLocalAssetsDirURLForSite("");
  306.     if (destDir[destDir.length-1] != "/")
  307.         destDir += "/";
  308.     var destLeafNameRaw = stripExtension(getLeafName(sourceURL));
  309.  
  310.     // convert any double-byte chars to single-byte.
  311.     // we can do this quietly (and sloppily) since the filename
  312.     // doesn't really have to be user-friendly, just unique.
  313.     var destLeafName = "";
  314.     for (i = 0; i < destLeafNameRaw.length; i++) 
  315.     {
  316.         var charCode = destLeafNameRaw.charCodeAt(i);
  317.         if (charCode > 255) 
  318.         {
  319.             // it's a doublebyte char. 
  320.             // since we're going to uniquify the name below, just
  321.             // convert all out-of-range chars into a lowercase 'a'
  322.             charCode = 97;
  323.         }
  324.         destLeafName += String.fromCharCode(charCode);
  325.     }
  326.  
  327.     var destURL = "";
  328.     for (var i = 0; i < 99999; ++i)
  329.     {
  330.         destURL = destDir;
  331.         destURL += destLeafName;
  332.         if (i != 0)
  333.             destURL += i.toString();
  334.         destURL += ".swf";
  335.         // strangely, DWfile.exists() doesn't decode urls (eg, %20->space), 
  336.         // so we must do that ourselves.
  337.         destURL = dw.doURLDecoding(destURL);
  338.         if (!DWfile.exists(destURL))
  339.         {
  340.             return destURL;
  341.         }
  342.     }
  343.  
  344.     return "";
  345. }
  346.  
  347. function waitForFlashPrinter(updatePercentageFunc, conversionInfo)
  348. {
  349.     if (conversionInfo.sessionID == 0)
  350.     {
  351.         var status = FlashPaperConnect.beginConversion(conversionInfo.sourceFile, 
  352.                                                         conversionInfo.destFile, 
  353.                                                         conversionInfo.paperSize);
  354.         if (isFpcError(conversionInfo.sessionID))
  355.         {
  356.             // doh. bail now.
  357.             return status;
  358.         }
  359.         else
  360.         {
  361.             // we're good; save the status as our sessionID and return null to continue waiting.
  362.             conversionInfo.sessionID = status;
  363.             updatePercentageFunc(-1, null);
  364.             return null;
  365.         }
  366.     }
  367.     else
  368.     {
  369.         var howLongToSleep = CONVERSION_CHECK_RATE / 2;
  370.         var status = FlashPaperConnect.getConversionStatus(conversionInfo.sessionID, howLongToSleep);
  371.         if (status == FPC_WAITING_FOR_CONNECTION)
  372.         {
  373.             // we're waiting for the FlashPrinter to respond.
  374.             updatePercentageFunc(-1, null);
  375.             return null;
  376.         }
  377.         else if (status == FPC_CONVERSION_IN_PROGRESS)
  378.         {
  379.             var percentageDone = FlashPaperConnect.getConversionPercentComplete(conversionInfo.sessionID);
  380.             var pageBeingProcessed = FlashPaperConnect.getConversionPageBeingProcessed(conversionInfo.sessionID);
  381.  
  382.             var msgString = null;
  383.             if (percentageDone < 0)
  384.             {
  385.                 msgString = dw.loadString("flashpaper/flash paper receiving message");
  386.  
  387.                 // in odd situations, we can get "Receiving page 0". Quietly check and fix.
  388.                 if (pageBeingProcessed == 0) 
  389.                 {
  390.                     msgString = dw.loadString("flashpaper/flash paper waiting message");
  391.                 }
  392.             }
  393.             else
  394.             {
  395.                 msgString = dw.loadString("flashpaper/flash paper converting message");
  396.                 
  397.                 // ideally we would have messages like this, but it's too late to add specific UI
  398.                 // string for this. we'll just use a generic message.
  399.                 //
  400.                 //if (pageBeingProcessed == 0) 
  401.                 //    msgString = "Optimizing...";
  402.                 //else if (percentageDone >= 100) 
  403.                 //    msgString = "Compressing...";
  404.  
  405.                 if (pageBeingProcessed == 0 || percentageDone >= 100) 
  406.                     msgString = dw.loadString("flashpaper/flash paper waiting message");
  407.             }
  408.  
  409.             var re = /%d/;
  410.             msgString = msgString.replace(re, pageBeingProcessed.toString());
  411.  
  412.             updatePercentageFunc(percentageDone, msgString);
  413.  
  414.             // we're still waiting.
  415.             return null;
  416.         }
  417.         else
  418.         {
  419.             return status;
  420.         }
  421.     }
  422. }
  423.  
  424. function addAccessibility(rtnStr) 
  425. {
  426.    var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/Object Options.htm";
  427.    var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
  428.  
  429.    cmdDOM.parentWindow.setFormItem(rtnStr);
  430.    dreamweaver.popupCommand("Object Options.htm");
  431.    return (cmdDOM.parentWindow.returnAccessibilityStr(rtnStr));    
  432. }
  433.  
  434.